package com.quanz.ckgl.mvc;

import static org.crank.crud.criteria.Comparison.eq;
import static org.crank.crud.criteria.Comparison.like;

import org.apache.commons.lang3.StringUtils;
import org.crank.crud.criteria.Criterion;
import org.crank.crud.criteria.Group;
import org.crank.crud.criteria.OrderBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.quanz.ckgl.services.Rack;
import com.quanz.ckgl.services.RackService;

/**
 * 货架管理Controlller
 * 
 * @author James Quan
 * @version 2015年10月9日 下午9:20:26
 */
@RestController
public class RackController {

	private RackService rackService;

	@Autowired
	public void setRackService(RackService rackService) {
		this.rackService = rackService;
	}

	@RequestMapping(value = "/racks_processing.json", method = { RequestMethod.GET, RequestMethod.POST })
	public DataTablesResponse racks_processing(@ModelAttribute DataTablesRequest request) {
		DataTablesSearch search = request.getSearch();
		Criterion criterion = null;
		if (search != null && !StringUtils.isEmpty(search.getValue())) {
			String v = "%" + search.getValue() + "%";
			criterion = Group.or(like("id", v), like("address", v));
		}
		int count = rackService.findRackCount(criterion);
		DataTablesResponse resp = new DataTablesResponse();
		resp.setDraw(request.getDraw());
		resp.setRecordsFiltered(count);
		resp.setTotalRecords(count);
		if (count > 0) {
			DataTablesOrder[] orders = request.getOrder();
			OrderBy[] orderBy = null;
			if (orders != null && orders.length > 0) {
				DataTablesColumn[] columns = request.getColumns();
				orderBy = new OrderBy[orders.length];
				for (int i = 0; i < orders.length; i++) {
					DataTablesOrder order = orders[i];
					String column = columns[order.getColumn()].getData();
					orderBy[i] = "asc".equals(order.getDir()) ? OrderBy.asc(column) : OrderBy.desc(column);
				}
			}
			Rack[] racks = rackService.findRack(orderBy, request.getStart(), request.getLength(), criterion);
			resp.setData(racks);
		} else {
			resp.setData(new Rack[0]);
		}
		return resp;
	}

	@RequestMapping("/rack/unused")
	public boolean checkRackIdUnused(@RequestParam("id") String id) {
		return rackService.findRackCount(eq("id", id)) == 0;
	}

	@RequestMapping("/rack/{id}")
	public Rack getRackById(@PathVariable String id) {
		Rack rack = rackService.findRackById(id);
		return rack;
	}

	@RequestMapping(value = "/rack", method = RequestMethod.POST)
	public Rack createRack(Rack rack) {
		Rack newRack = rackService.createRack(rack);
		return newRack;
	}

	@RequestMapping(value = "/rack/{id}", method = RequestMethod.PUT)
	public boolean updateRack(@PathVariable String id, @ModelAttribute Rack rack) {
		Rack r = rackService.findRackById(id);
		if (r == null) {
			throw new IllegalArgumentException("rack" + id + " not found!");
		}
		r = new Rack(r);
		r.setColor(rack.getColor());
		r.setIndex(rack.getIndex());
		r.setLayers(rack.getLayers());
		r.setCols(rack.getCols());
		r.setNote(rack.getNote());
		rackService.updateRack(r);
		return true;
	}

	@RequestMapping(value = "/rack/{id}", method = RequestMethod.DELETE)
	public boolean removeRack(@PathVariable String id) {
		Rack rack = rackService.findRackById(id);
		if (rack != null) {
			rackService.removeRack(rack);
			return true;
		} else {
			return false;
		}
	}

}
